﻿using System.Linq;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,Account")]
    public class QuanLyTaiKhoanController : Controller
    {
        //
        // GET: /Admin/QuanLyTaiKhoan/
        readonly CMSMVC4Entities _db = new CMSMVC4Entities();

        public ActionResult Index()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/QuanLyTaiKhoan").Rows[0]["Id"];
            var lac = _db.Accounts.Where(x => !x.Account1.Equals("Admin") || !x.Account1.Equals("admin")).ToList();
            return View(lac);
        }

        public ActionResult Xoa(string[] id)
        {
            if (id != null)
            {
                foreach (var i in id)
                {
                    if (!i.Equals("Admin") && !i.Equals("admin"))
                    {
                        var row = _db.Accounts.FirstOrDefault(x => x.Account1.Equals(i));
                        _db.Accounts.DeleteObject(row);
                        _db.SaveChanges();
                    }
                }
            }
            return Json(string.Empty, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Them()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/QuanLyTaiKhoan/Them").Rows[0]["Parrent"];
            var model = new ThemTaiKhoan();
            return View(model);
        }

        [HttpPost]
        public ActionResult Them(ThemTaiKhoan model)
        {
            if (ModelState.IsValid)
            {
                var ac = _db.Accounts.FirstOrDefault(x => x.Account1.Equals(model.Account));
                if (ac == null)
                {
                    if (string.IsNullOrEmpty(model.Roles))
                    {
                        ModelState.AddModelError("Roles", "Ban Phai Chon Quyen");
                    }
                    else
                    {
                        var row = new Account
                                      {
                                          Account1 = model.Account,
                                          Email = model.Email,
                                          Password = model.Pass,
                                          Role = model.Roles,
                                          Status = AccountStatus.Confirm
                                      };
                        _db.Accounts.AddObject(row);
                        _db.SaveChanges();
                        ModelState.AddModelError("", "Da Them Tai Khoan Thanh Cong");
                        model = new ThemTaiKhoan();
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Tai Khoan Da Ton Tai");
                }
            }
            return View(model);
        }

        public ActionResult Sua(string id)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/QuanLyTaiKhoan").Rows[0]["Id"];
            var ac = _db.Accounts.FirstOrDefault(x => x.Account1.Equals(id));
            if (ac != null)
            {
                if (!ac.Account1.Equals("Admin"))
                {
                    var model = new SuaTaiKhoan();
                    model.Account = ac.Account1;
                    model.Email = ac.Email;
                    model.Roles = ac.Role;
                    return View(model);
                }
            }
            return View("Error");
        }

        [HttpPost]
        public ActionResult Sua(SuaTaiKhoan model)
        {
            if (ModelState.IsValid)
            {
                var ac = _db.Accounts.FirstOrDefault(x => x.Account1.Equals(model.Account));
                if (ac != null)
                {
                    ac.Email = model.Email;
                    if (!string.IsNullOrEmpty(model.Pass))
                    {
                        ac.Password = model.Pass;
                    }
                    if (!string.IsNullOrEmpty(model.Roles))
                    {
                        ac.Role = model.Roles;
                    }
                    _db.SaveChanges();
                    //ModelState.AddModelError("", "Luu du lieu thanh cong");
                    //model.Roles = ac.Role;
                    return RedirectToAction("Sua", new {id = ac.Account1});
                }
            }
            return View(model);
        }
    }
}
